package com.practice.sequence;

public class FindLeastUnshown {
	public int find(int[] a) {
		int least = a.length;
		
		for (int i=a.length-1; i>=0; i--) {
			if (a[i]<=0 || a[i]>i+1) {
				least = i;
				continue;
			}
			
			if (a[i] != i+1) {
				if (a[i] == a[a[i]-1]) {
					least = i;
				}
				else {
					int t = a[i]-1;
					a[i] = a[t];
					a[t] = t+1;
					i++;
				}
			}
		}
		
		return least+1;
	}
}
